package com.cn.aam.checaiduo.backend.observer;

import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.widget.WMProgressBar;
import com.orhanobut.logger.Logger;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author 艹羊
 * @project Checaiduo_Android
 * @date 2017/6/5 下午5:56
 * @description 所有ObserVer的基类
 */

public abstract class BaseObserver<T> implements Observer<T> {
    protected Disposable disposable;
    protected BaseActivity baseActivity;

    protected String getProgressBarTitle() {
        return "请稍等";
    }

    protected String getProgressBarContent() {
        return "请求数据中...";
    }

    protected boolean isShowProgressBar() {
        return true;
    }

    public BaseObserver(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        //progressbar

        if (!isShowProgressBar()) {
            return;
        }
        String title = getProgressBarTitle();
        String content = getProgressBarContent();
        WMProgressBar.showProgressDialog(baseActivity, title, content);
    }

    @Override
    public void onSubscribe(Disposable d) {
        this.disposable = d;
        baseActivity.addDisposable(d);
    }

    @Override
    public void onComplete() {
        WMProgressBar.hideProgressDislog();
    }

    @Override
    public void onError(Throwable e) {
        Logger.e("错误 " + e.toString());
        WMProgressBar.hideProgressDislog();
    }
}
